export const handleAfterRead = function(file, context) {
  // 处理文件上传
  context.fileList.push({ url: file.file.url });
  return context.uploadFile(file.file).then(res => {
    context.filePath.push(res.data.fileList[0]);
  }).catch(error => {
    console.error('文件上传失败', error);
  });
};

export const handleSyncCustomerChange = function(value, context) {
  context.syncCustomerValue = value;
};

export const handleNeedReservationChange = function(value, context) {
  context.needReservationValue = value;
};

export const handleChannelChange = function(value, context) {
  context.channelValue = value;
};

export const handleStockLimitChange = function(value, context) {
  context.stockLimitValue = value;
};

export const handleIsLimitedPurchaseChange = function(value, context) {
  context.isLimitedPurchaseValue = value;
};

export const handleDateTypeChange = function(value, context) {
  context.dateTypeValue = value;
};

export const uploadFile = function(file) {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: 'http://www.chifutest.com/api/common/upload',
      filePath: file.url,
      name: 'file',
      formData: {
        'token': 'your-token'
      },
      success: (res) => {
        if (res.statusCode === 200) {
          const response = JSON.parse(res.data);
          if (response.code === 1) {
            const fullUrl = response.data.url;
            resolve({ data: { fileList: [{ url: fullUrl }] } });
          } else {
            reject(new Error('文件上传失败'));
          }
        } else {
          reject(new Error('文件上传失败'));
        }
      },
      fail: (error) => {
        reject(error);
      }
    });
  });
};
